$(function(){
	
	var $btnRegister = $('#btnRegister');
	$btnRegister.click(function(){
		Register();
	});
});

//注册
function Register(){
	
	var $name			= $('#name');
	var $passwd			= $('#passwd');
	var $repasswd		= $('#repasswd');
	var $email			= $('#email');
	var $mobile_phone	= $('#mobile_phone');
	var $location		= $('#location');
	
	var name		= $.trim($name.val());
	var passwd		= $.trim($passwd.val());
	var repasswd	= $.trim($repasswd.val());
	var email		= $.trim($email.val());
	var mobile_phone= $.trim($mobile_phone.val());
	var location	= $location.val();
	
	//验证注册信息
	var error = 0;
	
	if(!nameCheck(name)){
		$name.focus();
		error++;
	}
	
	if(error > 0){
		return false;;
	}
	
	
	
	
	//密码
	if(!passwdCheck(passwd)){
		$passwd.focus();
		error++;
	}else if(passwd != repasswd){
		alert('两次密码输入不一致!');
		error++;
	}
	
	if(error > 0){
		return false;;
	}
	

	//邮箱
	if(!isEmail(email)){
		alert('邮箱格式不正确');
		$email.focus();
		error++;
	}
	if(error > 0){
		return false;;
	}
	
	//手机
	if(!isMobilePhone(mobile_phone)){
		alert('手机号格式不正确');
		$mobile_phone.focus();
		error++;
	}
	
	
	
	//有验证不通过的不执行
	if(error > 0){
		return false;;
	}
	
	
	var url = '/index.php/Member/register_do';
	var params = {
		'name'			:name,
		'passwd'		:passwd,
		'email'			:email,
		'mobile_phone'	:mobile_phone,
		'location'		:location
	};
	
	$.post(url,params,function(data){
		if(data.success){
			alert('注册成功!');
			window.location.href = '/index.php/Member/profile/';
		}else{
			alert('注册失败!');
		}
	},'json');
}

//检查用户名
function nameCheck(name){
	
	if (name.length < 4 || name.length > 20){
		alert('用户名长度在4-20个字符!');
		return false;
	}
	
	
	//用户名不能只包含数字!
	$numName = (new RegExp(/^[0-9]+$/).test(name));
	
	if($numName){
		alert('用户名不能只包含数字!');
		return false;
	}
	
	//用户名只能包含中文，数字，英文字母，下划线以及横线!
	$nameFormat = (new RegExp(/^[0-9a-z_\-]+$/i).test(name));
	
	if(!$nameFormat){
		alert('用户名只能包含，英文字母数字，下划线以及横线!');
		return false;
	}
	
	return true;
}


//检查用户名
function passwdCheck(passwd){
	if (passwd.length < 6 || passwd.length > 20){
		alert('密码长度在6-20个字符!');
		return false;
	}
	return true;
}
